const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const tcb = require("@cloudbase/node-sdk")

const app = tcb.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const auth = app.auth();

const axios = require('axios')
const APPID = "wxb5023b51f43bc119"  //换成你的小程序appid
const APPSECRET = "dd6227f5ed0c026dcbc09c2028ad2938" //换成你的小程序key
exports.main = async (event, context) => {
  const { userInfo } = await auth.getEndUserInfo();
  console.log(userInfo);
  const { uid } = userInfo

  const tokenurl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`
  const res = await axios.get(tokenurl)
  const {access_token} = res.data

  const qrcodeurl=`https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${access_token}`
  const wxacodeResult = await axios({
    method: 'post',
    url: qrcodeurl,
    responseType: 'arraybuffer',
    data: {
      scene:uid,
      page:"pages/login/login"
    }
  });
  console.log('erweima',wxacodeResult)
  const uploadResult = await cloud.uploadFile({
    cloudPath: `weblogin/${uid}/qr.jpg`,
    fileContent: wxacodeResult.data
  })


  return {    
    uid:uid,
    fileId:uploadResult.fileID
  }

}